#article/views.py
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.views import APIView

from blog.article_model import Article,MySqlSession

#定义返回结果标准类
class Result:
    def __init__(self,code,message,data):
        self.code = code
        self.message = message
        self.data = data
    def to_json(self):
        return {
            "code":self.code,
            "message":self.message,
            "data":self.data
        }

#Article: title,author,price,content,pub_date,status
#序列化类及验证
class ArticleSerializer(serializers.Serializer):
    title = serializers.CharField(max_length=100,required=True)
    author = serializers.CharField(max_length=100)
    price = serializers.FloatField(required=True,min_value=0,max_value=1000)
    content = serializers.CharField(required=False)
    pub_date = serializers.DateField(required=True)
    status = serializers.IntegerField(default=1)



class ArticleForm(APIView):
    """文章列表"""
    def post(self, request):
        #验证表单
        serializer = ArticleSerializer(data=request.data)
        if not serializer.is_valid():
            print("表单验证失败：",serializer.errors)
            #组装返回结果
            result = Result(400,"fail",serializer.errors)
            #返回json数据
            return Response(result.to_json(),400)
        print("/article/form/ post请求")
        #打印出request.post数据
        print("打印出request.POST数据：",request.data)
        session = MySqlSession().get_session()
        #表单提交的数据结果：title,author,price,content,pub_date,status
        #插入数据
        article = Article(
            title=request.data.get('title'),
            author=request.data.get('author'),
            price=request.data.get('price'),
            content=request.data.get('content'),
            pub_date=request.data.get('pub_date'),
            status=request.data.get('status')
        )
        session.add(article)
        session.commit()
        session.close()
        #组装返回结果
        # result = {
        #     "code":200,
        #     "message":"success",
        #     "data":"插入成功"
        # }
        result = Result(200,"success","插入成功")
        #返回json数据
        return Response(result.to_json(),200)

class ArticleDetailPost(APIView):
    """文章列表"""
    def post(self, request):
        print("/article/detail/post==> post请求")
        print("打印出request.data：",request.data)
        #打印request所有参数
        print("打印出request.query_params：",request.query_params)
        #打印出request 头部信息
        print("打印出request.headers：",request.headers)
        #表单形式数据的解析
        print("打印出request.data：",request.data.get('title'))
        #组装返回结果
        result = {
            "code":200,
            "message":"success",
            "data":request.data
        }
        #返回json数据
        return Response(result)


class ArticleDetail(APIView):
    """文章详情"""
    def get(self,request,id):
        print("/article/detail/ get请求")
        #获取get请求中的参数
        print("打印出request.GET数据：",request.GET)
        print("打印出request.GET数据：",request.GET.get('title'))
        print("打印出request.query_params：",request.query_params.get('title'))
        #解析上下文的参数
        print("打印出request.parser_context：",request.parser_context)
        # print("打印出request.parser_context：",request.parser_context['kwargs']['title'])
        #打印ID
        print("打印出id：",id)
        #返回json数据
        return Response({"path":"/article/detail/"})

